package com.rq.adapter.ui;

import android.app.Activity;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.rq.adapter.R;
import com.rq.rvlibrary.BaseAdapter;
import com.rq.rvlibrary.BaseViewHolder;
import com.rq.rvlibrary.EasyViewHolder;
import com.rq.rvlibrary.OnItemClickListener;
import com.rq.rvlibrary.RecyclerUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class BaseUsingActivity extends Activity implements OnItemClickListener {

    BaseAdapter adapter;
    BaseAdapter clickAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_base);
        adapter = new BaseAdapter(this, R.layout.item_red) {
            @Override
            protected void onBindEasyHolder(BaseAdapter adapter, BaseViewHolder holder, int position, Object o) {
                super.onBindEasyHolder(adapter, holder, position, o);
                holder.setItemText(R.id.txt, (String) o);
            }
        };
        new RecyclerUtil(adapter).set2View((RecyclerView) findViewById(R.id.recycler));//默认垂直
//        new RecyclerUtil(adapter).horizontal().set2View((RecyclerView) findViewById(R.id.recycler));--水平滑动
//        new RecyclerUtil(adapter).row(3).set2View((RecyclerView) findViewById(R.id.recycler)); --网格布局
        new Thread(new Runnable() {//模拟网络请求
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.setData(getDebugData());
                    }
                });
            }
        }).start();
        clickAdapter = new BaseAdapter(this, R.layout.item_clcik) {
            @Override
            protected void onBindEasyHolder(BaseAdapter adapter, BaseViewHolder holder, int position, Object o) {
                super.onBindEasyHolder(adapter, holder, position, o);
                ((TextView) holder.itemView).setText((String) o);
            }
        };
        new RecyclerUtil(clickAdapter).horizontal().set2View((RecyclerView) findViewById(R.id.btn));
        clickAdapter.addOnItemClickListener(this);
        List<String> btns = new ArrayList<>();
        btns.add("水平布局");
        btns.add("网格布局");
        btns.add("条件展示");
        btns.add("多样式展示");
        btns.add("垂直布局");
        clickAdapter.setData(btns);
    }

    private List<String> getDebugData() {
        List<String> test = new ArrayList<>();
        test.add("123456");
        test.add("1");
        test.add("2");
        test.add("3");
        test.add("4");
        test.add("5");
        test.add("11");
        test.add("12");
        test.add("13");
        test.add("14");
        test.add("15");
        test.add("21");
        test.add("22");
        test.add("23");
        test.add("24");
        test.add("25");
        test.add("26");
        test.add("27");
        test.add("28");
        test.add("29");
        test.add("30");
        return test;
    }

    @Override
    public void onItemClick(BaseAdapter a, BaseViewHolder holder, Object o, View v, int position) {
        if ("垂直布局".equals(o)) {
            new RecyclerUtil(adapter).vertical().set2View((RecyclerView) findViewById(R.id.recycler));
        } else if ("水平布局".equals(o)) {
            new RecyclerUtil(adapter).horizontal().set2View((RecyclerView) findViewById(R.id.recycler));
        } else if ("网格布局".equals(o)) {
            new RecyclerUtil(adapter).row(3).set2View((RecyclerView) findViewById(R.id.recycler));//网格布局
        } else if (position == 2) {
            adapter.setDisplay(new BaseAdapter.DisplayOption() {
                @Override
                public boolean show(Object bean, @NonNull Object rule, int position) {
                    if (bean instanceof String) {
                        return ((String) bean).contains(String.valueOf(rule));
                    }
                    return true;
                }
            });
            int rule = new Random().nextInt(10);
            ((TextView) holder.itemView).setText("当前rule为:" + (rule == 0 ? null : rule));
            adapter.display(rule == 0 ? null : rule);
        } else if ("多样式展示".equals(o)) {
            SparseArray<Class> layouts = new SparseArray<>();
            //     此处方便演示仅使用 EasyViewHolder  有复杂显示逻辑等要求可继承自 BaseViewHolder 并自行实现逻辑
            layouts.put(R.layout.item_red, EasyViewHolder.class);
            layouts.put(R.layout.item_purple, EasyViewHolder.class);//  这俩layout id 一致,只有样式差异
            layouts.put(R.layout.item_clcik, EasyViewHolder.class);//   这俩layout id 一致,只有样式差异
            adapter = new BaseAdapter(BaseUsingActivity.this, layouts) {

                @Override
                protected int getMultipleHolderType(Object dataItem, int position) {
                    if (position < 3) {
                        return R.layout.item_clcik;
                    }
                    if (((String) dataItem).contains("2")) {
                        return R.layout.item_red;
                    }
                    return R.layout.item_purple;
                }

                @Override
                protected void onBindEasyHolder(BaseAdapter adapter, BaseViewHolder holder, int position, Object o) {
                    super.onBindEasyHolder(adapter, holder, position, o);
                    if (position < 3) {
                        ((TextView) holder.itemView).setText((String) o);
                    } else {
                        holder.setItemText(R.id.txt, (String) o);
                    }
                }
            };
            new RecyclerUtil(adapter).set2View((RecyclerView) findViewById(R.id.recycler));
            adapter.setData(getDebugData());
        }
    }
}
